package woc.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import woc.data.Person;
import woc.data.User;
import woc.data.WorkExp;
import woc.data.WorkExpProperty;
import woc.data.WorkExpPropertyValue;
import woc.data.custom.WorkExpPropertyKeyValue;
import woc.service.WorkService;
import woc.service.base.RootServiceImpl;

public class WorkServiceImpl extends RootServiceImpl implements WorkService {

	public List<WorkExpProperty> getWorkExpPropertyList() {
		return getWorkExpPropertyDAO().find("from WorkExpProperty WHERE deleted = false ORDER BY sequence ASC");
	}

	public void saveOrUpdate(List<List<WorkExpPropertyKeyValue>> workExperienceList,User currentUser, Person person) {
		
		int sequence=0;
		for (Iterator<List<WorkExpPropertyKeyValue>> iterator = workExperienceList.iterator(); iterator.hasNext();) {
			
			List<WorkExpPropertyKeyValue> workExpPropertyKeyValueList = iterator.next();
			
			
			if(workExpPropertyKeyValueList.size()>0){
				WorkExp tbWorkExp =null;
				WorkExpPropertyKeyValue tmp=workExpPropertyKeyValueList.get(0);
				tbWorkExp = tmp.getWorkExp();
				
				if(tbWorkExp.getId()==null){
					tbWorkExp.setCreateUserId(currentUser.getId());
					tbWorkExp.setPersonId(person.getId());
					tbWorkExp.setSequence(sequence);
					tbWorkExp.setUpdateDate(new Date());
					tbWorkExp.setUpdateUserId(currentUser.getId());	
				}else{
					tbWorkExp.setSequence(sequence);
					tbWorkExp.setUpdateDate(new Date());
					tbWorkExp.setUpdateUserId(currentUser.getId());	
				}				
				getWorkExpDAO().saveOrUpdate(tbWorkExp);
				sequence++;
				
				for (Iterator<WorkExpPropertyKeyValue> iterator2 = workExpPropertyKeyValueList.iterator(); iterator2.hasNext();) {
					WorkExpPropertyKeyValue workExpPropertyKeyValue = iterator2.next();					
					
					WorkExpPropertyValue tbWorkExpPropertyValue = workExpPropertyKeyValue.getWorkExpPropertyValue();
					
					tbWorkExpPropertyValue.setCreateUserId(currentUser.getId());
					tbWorkExpPropertyValue.setUpdateDate(new Date());
					tbWorkExpPropertyValue.setUpdateUserId(currentUser.getId());				
					tbWorkExpPropertyValue.setWorkExpId(tbWorkExp.getId());
					tbWorkExpPropertyValue.setWorkExpPropertyId(workExpPropertyKeyValue.getWorkExpProperty().getId());
					
					getWorkExpPropertyValueDAO().saveOrUpdate(tbWorkExpPropertyValue);				
				}
			}
		}
	}

	public WorkExpPropertyValue getWorkExpPropertyValue(Integer workExpId,Integer workExpPropertyId) {
		List<WorkExpPropertyValue> valueList = getWorkExpPropertyValueDAO().find("from WorkExpPropertyValue WHERE " +
				" deleted=false AND workExpId="+workExpId+" AND" +
				" workExpPropertyId ="+workExpPropertyId);
		if(valueList!=null && valueList.size()>0){
			return (WorkExpPropertyValue) valueList.get(0);
		}else{
			return null;
		}
	}

	public List<WorkExp> getWorkExpListByPersonId(Integer personId) {
		return getWorkExpDAO().find("from WorkExp where deleted=false AND personId="+personId);
	}

	public void saveOrUpdate(WorkExp workExp) {
		getWorkExpDAO().saveOrUpdate(workExp);	
	}

	public void saveOrUpdate(WorkExpPropertyValue workExpPropertyValue) {
		getWorkExpPropertyValueDAO().saveOrUpdate(workExpPropertyValue);
	}

	public void deleteWorkExpPropertyById(Integer entityId, User currentUser) {
		WorkExpProperty entity = getWorkExpPropertyDAO().get(entityId);
		entity.setUpdateDate(new Date());
		entity.setUpdateUserId(currentUser.getId());
		entity.setDeleted(true);		
		getWorkExpPropertyDAO().saveOrUpdate(entity);		
	}

	public WorkExpProperty getWorkExpPropertyById(Integer entityId) {
		return getWorkExpPropertyDAO().get(entityId);
	}

	public void saveOrUpdate(WorkExpProperty entity) {
		getWorkExpPropertyDAO().saveOrUpdate(entity);
	}
}
